package simple;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * 题号：20
 * 题目名称：有效的括号
 */
public class VaildComma {
    public static boolean solution(String str){
        Map<Character,Character> map = new HashMap<>(3);
        map.put(']','[');
        map.put(')','(');
        map.put('}','{');
        Deque<Character> stack = new LinkedList<>();
        int len = str.length();
        if(len % 2 != 0 ){
            return false;
        }
        for(int i = 0;i<len;i++){
            Character ch = str.charAt(i);
            if(map.containsKey(ch)){
                if(!stack.isEmpty() && map.get(ch).equals(stack.peek())){
                    stack.pop();
                }
            }else{
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String str = "{}(]";
        System.out.println(solution(str));
    }
}
